package com.taptech.doufu.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.activity.story.StoryCodeActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.CommonFragmentPagerAdapter;
import com.taptech.doufu.ui.fragment.CategoryFragment;
import com.taptech.doufu.ui.fragment.story.MyStoryFragment;
import com.taptech.doufu.ui.fragment.weex.SimpleWeexFragment;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.ui.view.tablayout.TabLayout;
import com.taptech.doufu.util.TMAnalysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class MyWorksActivity extends DiaobaoBaseActivity {

    private TabLayout tlTitle;
    private TTHomeViewPager mViewPager;
    List<Fragment> list = new ArrayList<>();
    private int currPosition = 0;
    private ImageView ivAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_works_layout);
        tlTitle = (TabLayout) findViewById(R.id.tlTitle);
        mViewPager = (TTHomeViewPager) findViewById(R.id.mViewPager);
        ivAdd = findViewById(R.id.ivAdd);
        mViewPager.setOffscreenPageLimit(10);
        int normalColor = getResources().getColor(R.color.text_color_2);
        int selectColor = getResources().getColor(R.color.text_color_57);
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            normalColor = getResources().getColor(R.color.text_dark);
            selectColor = getResources().getColor(R.color.theme_main_dark);
        }
        tlTitle.setTabTextColors(normalColor,selectColor);
        tlTitle.setSelectedTabIndicatorColor(selectColor);
        currPosition = getIntent().getIntExtra(Constant.POSITION, 0);
        ivAdd.setOnClickListener(v -> {
            if (currPosition == 0) {
                Intent intentCreateNovel = new Intent(this, NovelCreateTosActivity.class);
                startActivity(intentCreateNovel);
            }
//            else if (currPosition == 1) {
//                if (!AccountService.hasBindMobile()) {
//                    presentWeexView("vue/personalcenter/TFBindMobileAlert.js?present=1");
//                }else{
//                    StoryCodeActivity.Companion.startActivity(this);
//                }
//            } else if (currPosition == 2) {
//                SimpleWeexActivity.startActivity(this,"vue/find/TFTopicPublish.js?needLogin=1");
//            }
        });

        String[] titles = new String[2];
        titles[0] = "小说";
//        titles[1] = "小故事";
//        titles[2] = "帖子";
        titles[1] = "文评";

        list.add(SimpleWeexFragment.newInstance("vue/personalcenter/TFMyNovelList.js"));
//        list.add(MyStoryFragment.Companion.newInstance());
//        list.add(SimpleWeexFragment.newInstance("vue/find/common/TFMyPostList.js"));
        list.add(SimpleWeexFragment.newInstance("vue/articalComment/TFMyArticalComment.js"));
        CommonFragmentPagerAdapter adapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), list, titles);
        mViewPager.setAdapter(adapter);
        tlTitle.setupWithViewPager(mViewPager);
        mViewPager.setCurrentItem(currPosition);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currPosition = position;
                if (position == 1) {
                    ivAdd.setVisibility(View.GONE);
                }else{
                    ivAdd.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context,MyWorksActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, int position) {
        Intent intent = new Intent();
        intent.setClass(context,MyWorksActivity.class);
        intent.putExtra(Constant.POSITION,position);
        context.startActivity(intent);
    }
}
